



************************************************************;
** Create Matched HEDIS2005 matched with Denominator file **;
************************************************************;

options ls=76 nofmterr;

libname raw '/data/Medicare_P01_2009/data/HEDIS/rawdata';
libname new '/data/Medicare_P01_2009/data/HEDIS/sasdata';
libname plan '/data/Medicare_P01_2009/data/HEDIS/plandata';

data a; set raw.hedis2005data;

     length hic_org $ 18 ;
     hic_org=org_id||'-'||hic_number;

     ** Invalid HIC number Indicators **;

     if substr(hic_number,2,11)=' ' or
        substr(hic_number,3,10)=' ' or 
        substr(hic_number,4,9) =' ' or
        substr(hic_number,5,8) =' ' then short_hic=1; else short_hic=0;

      if hic_number=' ' or
         hic_number='00000' or
         hic_number='000000' or
         hic_number='0000000' or
         hic_number='00000000' or
         hic_number='000000000' or
         hic_number='0000000000' or
         hic_number='00000000000' or
         hic_number='000000000000' or

         hic_number='99999' or
         hic_number='999999' or
         hic_number='9999999' or
         hic_number='99999999' or
         hic_number='999999999' or
         hic_number='9999999999' or
         hic_number='99999999999' or
         hic_number='999999999999' or  
         hic_number='Error'   or
         hic_number='UNKNOWN' then invalid_hic=1; else invalid_hic=0; 

         proc freq;
              tables short_hic invalid_hic;
              title 'HEDIS2005 HIC Number of M_Months Check';
    
  data a01; set a;

       **** Exclude Invalid Cases ****;

       if short_hic=1   then delete;
       if invalid_hic=1 then delete;

       proc freq; 
            tables m_months;

   data a0; set a01;

       if m_months=0 then delete;
     
       proc sort; by hic_org;

    data a1; set a0;

         proc means n sum nway noprint;
              class hic_org;
              var m_months;
              output out=totmon0 sum=totmonth;

    data totmon; set totmon0;
         
         keep hic_org totmonth;

         proc sort; by hic_org;
 

       *** Add Plan Type Information ***;

      data plan0; set plan.hedis2005planinfo;
           org_id    = cms_contract_number ;
           plan_type = general_11;

        data plan; set plan0;
             keep org_id plan_type;
             proc sort; by org_id;

    data a00; merge a0 totmon; by hic_org;

         proc sort; by org_id;

    data a02; merge a00(in=ok) plan; by org_id;

         if ok;

         proc freq;
              tables totmonth;

    data a2; set a02;

         if totmonth>12 then delete; /** drop cases with more than 1 year months in one contract **/
         id_num=_n_;

         drop short_hic invalid_hic;
        
         proc sort; by hic_number;


     *** Outpupt LINKID2005 ***;
 
     data raw.linkid2005; set a2;

          keep hic_number id_num;

     *** Prepare crosswalk file ***;

     data xwalk00; set raw.hic_bene_xwalk_2005;
  
         if hic_match=1;
         if bene_id=' ' then delete;

         length hic_number $ 12;
         hic_number=orig_hic;

         proc sort; by hic_number;

    data xwalk0; set xwalk00;

         proc freq data=xwalk0 noprint;
              tables hic_number/out=benecnt;  
       
         proc freq data=benecnt;
              tables count;
              title 'HEDIS2005: Num of BENE links for one HIC number';

    data xwalk(drop=count); merge xwalk00 benecnt; by hic_number;
       
         if count=1; /** only keep cases with one unique BENE ID **/

         keep hic_match hic_number bene_id count;

         proc sort; by hic_number;


data b; merge a2(in=ok) xwalk; by hic_number;

        if ok;

        if hic_match=. then hic_match=0;
     
        proc sort; by bene_id;

data denom; set raw.denom2005;

     denom_flag=1;
     
     sex  = 1*BENE_SEX_IDENT_CD;
     race = 1*BENE_RACE_CD;          
     age  = BENE_AGE_AT_BEG_REF_YR;

     length racec gender agec $ 10;

     if race=1 then racec='1.White';
     if race=2 then racec='2.Black';
     if race=3 then racec='3.Other';
     if race=4 then racec='4.Asian';
     if race=5 then racec='5.Hispanic';
     if race=6 then racec='6.Native';
     if race=. or race=0 then racec='0.Unknown';

     if sex=1 then gender='1.Male';
     if sex=2 then gender='2.Female';

     if age>0   and age<20 then agec='1-20 yr';
     if age>=20 and age<30 then agec='20-29 yrs';
     if age>=30 and age<40 then agec='30-39 yrs';
     if age>=40 and age<50 then agec='40-49 yrs';
     if age>=50 and age<60 then agec='50-59 yrs';
     if age>=60 and age<65 then agec='60-64 yrs';
     if age>=65 and age<70 then agec='65-69 yrs';
     if age>=70 and age<75 then agec='70-74 yrs';
     if age>=75 and age<80 then agec='75-79 yrs';
     if age>=80 and age<85 then agec='80-84 yrs';
     if age>=85            then agec='85+';

    **-- Creat Age 10-year category --**;

     agen=1*age;

     length agec2 $ 5;

     if agen>=65 and agen<75 then agec2='65-74';
     if agen>=75 and agen<85 then agec2='75-84';
     if agen>=85             then agec2='85+';

     proc sort nodupkey; by bene_id;


data hed05(drop=hic_number hic_org); merge b(in=ok) denom; by bene_id;

     if ok;

     if denom_flag=. then denom_flag=0;

     *** Add State and Region Variables ***;

    if state_code='01' then state_ab='AL' ;    if state_code='02' then state_ab='AK' ;
    if state_code='03' then state_ab='AZ' ;    if state_code='04' then state_ab='AR' ;
    if state_code='05' then state_ab='CA' ;    if state_code='06' then state_ab='CO' ;
    if state_code='07' then state_ab='CT' ;    if state_code='08' then state_ab='DE' ;
    if state_code='09' then state_ab='DC' ;    if state_code='10' then state_ab='FL' ;

    if state_code='11' then state_ab='GA' ;    if state_code='12' then state_ab='HI' ;
    if state_code='13' then state_ab='ID' ;    if state_code='14' then state_ab='IL' ;
    if state_code='15' then state_ab='IN' ;    if state_code='16' then state_ab='IA' ;
    if state_code='17' then state_ab='KS' ;    if state_code='18' then state_ab='KY' ;
    if state_code='19' then state_ab='LA' ;    if state_code='20' then state_ab='ME' ;

    if state_code='21' then state_ab='MD' ;    if state_code='22' then state_ab='MA' ;
    if state_code='23' then state_ab='MI' ;    if state_code='24' then state_ab='MN' ;
    if state_code='25' then state_ab='MS' ;    if state_code='26' then state_ab='MO' ;
    if state_code='27' then state_ab='MT' ;    if state_code='28' then state_ab='NE' ;
    if state_code='29' then state_ab='NV' ;    if state_code='30' then state_ab='NH' ;

    if state_code='31' then state_ab='NJ' ;    if state_code='32' then state_ab='NM' ;
    if state_code='33' then state_ab='NY' ;    if state_code='34' then state_ab='NC' ;
    if state_code='35' then state_ab='ND' ;    if state_code='36' then state_ab='OH' ;
    if state_code='37' then state_ab='OK' ;    if state_code='38' then state_ab='OR' ;
    if state_code='39' then state_ab='PA' ;    if state_code='40' then state_ab='PR';

    if state_code='41' then state_ab='RI' ;    if state_code='42' then state_ab='SC' ;
    if state_code='43' then state_ab='SD' ;    if state_code='44' then state_ab='TN' ;
    if state_code='45' then state_ab='TX' ;    if state_code='46' then state_ab='UT' ;
    if state_code='47' then state_ab='VT' ;    if state_code='48' then state_ab='GU';
    if state_code='49' then state_ab='VA' ;    if state_code='50' then state_ab='WA' ;

    if state_code='51' then state_ab='WV' ;    if state_code='52' then state_ab='WI' ;
    if state_code='53' then state_ab='WY' ;    

    length region9 region4 $ 25; 

    if state_ab in ('CT','MA','ME','NH','RI','VT')                then region9='1.New England';
    if state_ab in ('NJ','NY','PA')                               then region9='2.Mid Atlantic'; 
    if state_ab in ('WI','MI','IL','IN','OH')                     then region9='3.East North Central'; 
    if state_ab in ('ND','SD','NE','KS','MN','IA','MO')           then region9='4.West North Central'; 
    if state_ab in ('DE','MD','DC','FL','GA','NC','SC','VA','WV') then region9='5.South Atlantic';
    if state_ab in ('KY','TN','MS','AL')                          then region9='6.East South Central'; 
    if state_ab in ('OK','AR','TX','LA')                          then region9='7.West South Central';
    if state_ab in ('MT','ID','WY','NV','UT','CO','AZ','NM')      then region9='8.Mountain'; 
    if state_ab in ('WA','OR','CA','HI','AK')                     then region9='9.Pacific';

    if region9='1.New England' or 
       region9='2.Mid Atlantic'       then region4='Region 1: Northeast';

    if region9='3.East North Central' or 
       region9='4.West North Central' then region4='Region 2: Midwest';

    if region9='5.South Atlantic' or 
       region9='6.East South Central' or 
       region9='7.West South Central' then region4='Region 3: South';

    if region9='8.Mountain' or 
       region9='9.Pacific'            then region4='Region 4: West';

     zip5=1*substr(bene_zip_cd,1,5);

     proc sort; by zip5;


data zip; set new.zipinfo;

     proc sort; by zip5;


data new.hedis2005; merge hed05(in=ok) zip; by zip5;

     if ok;

     if sub_id=6626 then org_id='HS524';
     if sub_id=6628 then org_id='HN524';
     
     proc contents data=new.hedis2005 varnum;
          title 'HEDIS2005 analysis dataset';

     proc freq;
          tables totmonth denom_flag plan_type;

endsas;

